﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumProject.Models;
using ScrumProject.ViewModels;

namespace ScrumProject.Controllers
{ 
    public class ProjectWallController : Controller
    {
        private SMRPO6Context db = new SMRPO6Context();

        UserRepository userR = new UserRepository();
        MembersRepository memberR = new MembersRepository();
        PostRepository postR = new PostRepository();
        CommentRepository commentR = new CommentRepository();

        //
        // GET: /ProjectWall/
       
        public ActionResult Index(Post post)
        {
            var posts = db.Posts.Include(p => p.Project);
            var viewModel = new PostsViewModel();
            
            int id = int.Parse(this.ControllerContext.HttpContext.Request.Cookies["projectId"].Value);
            viewModel.Posts = postR.GetAllPostsByProject(id).OrderByDescending(d => d.Time);

            ViewBag.ViewUser = (User as CustomPrincipal).Username;
            ViewBag.UserId = (User as CustomPrincipal).UserId;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Index(Post post, FormCollection Form)
        {
            CustomPrincipal user = HttpContext.User as CustomPrincipal;
            
            try 
            {
                if (ModelState.IsValid)
                {
                    int id = int.Parse(this.ControllerContext.HttpContext.Request.Cookies["projectId"].Value);
                    Project project = db.Projects.Find(id);
                    user.Members = db.Members.Where(pr => pr.ProjectId == id).Where(pr => pr.UserId == user.UserId).FirstOrDefault();

                    string username = postR.GetUserWhoWrotePost(user.UserId);

                    post.UserId = user.UserId;
                    post.ProjectId = project.Id;
                    post.Time = DateTime.Now;

                    postR.AddPost(post);

                    ViewBag.user = username;
                    return RedirectToAction("Index");
                }
            }

            catch (NullReferenceException)
            {
                return RedirectToAction("Index");
            }
           
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", post.ProjectId);
            return View(post);
        }

        //
        // GET: /ProjectWall/AddComment

        public ActionResult AddComment(int? id)
        {
            Post post = db.Posts.Find(id);
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name");
            return View();
        }

        //
        // POST: /ProjectWall/AddComment

        [HttpPost]
        public ActionResult AddComment(Comment comment, string submitButton)
        {
            CustomPrincipal user = HttpContext.User as CustomPrincipal;

            if (!ModelState.IsValid)
            {
                if (submitButton == "Cancel")
                {
                    return RedirectToAction("Index", "ProjectWall");
                }
                else
                {
                    comment.Time = DateTime.Now;
                    comment.UserId = user.UserId;

                    db.Comments.Add(comment);
                    db.SaveChanges();
                    //commentR.AddCommentToPost(comment);
                    return RedirectToAction("Index", "ProjectWall");
                }
            }

            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", comment.Post.ProjectId);
            return View(comment);
        }
        
        //
        // GET: /ProjectWall/Delete/5

        [Authorize(Roles = "admin")]
        public ActionResult DeletePost(int? id)
        {
            Post post = db.Posts.Find(id);
            return View(post);
        }

        //
        // POST: /ProjectWall/Delete/5
        [Authorize(Roles = "admin")]
        [HttpPost, ActionName("DeletePost")]
        public ActionResult DeleteConfirmed(int? id, string submitButton)
        {
            if (submitButton == "Cancel")
            {
                return RedirectToAction("Index", "ProjectWall");
            }

            else
            {
                Post post = db.Posts.Find(id);

                db.Posts.Remove(post);
                db.SaveChanges();

                return RedirectToAction("Index", "ProjectWall");
            }
        }

        
        public ActionResult DeleteComment(int? cid)
        {
            Comment comment = db.Comments.Find(cid);
            return View(comment);
        }

        //
        // POST: /ProjectWall/Delete/5
        
        [HttpPost, ActionName("DeleteComment")]
        public ActionResult DeleteCommentConfirm(int? cid, string submitButton)
        {
            if (submitButton == "Cancel")
            {
                return RedirectToAction("Index", "ProjectWall");
            }

            else
            {
                Comment comment = db.Comments.Find(cid);

                db.Comments.Remove(comment);
                db.SaveChanges();
                
                return RedirectToAction("Index", "ProjectWall");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}